package DataBase;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import DataBaseInterface.BookServerInterface;
import DataBaseInterface.CustomerServerInterface;
import DataBaseInterface.ManagerServerInterface;
import DataBaseInterface.OrderServerInterface;
import DataBaseInterface.PromotionServerInterface;

public class DB_Factory{
	private static BookServerInterface bookServerDB;
	private static CustomerServerInterface customerServerDB;
	private static OrderServerInterface orderServerDB;
	private static ManagerServerInterface managerServerDB;
	private static PromotionServerInterface promotionServerDB;
	
	public static void initDB(InputStreamReader in){
		String address = null;
		try {
			BufferedReader reader = new BufferedReader(in);
			reader.readLine();
			address = reader.readLine();
			reader.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://"+address+"/bsso?useUnicode=true&characterEncoding=GBK";
		String user = "root";
		String password = "sky";
		try {
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			if(!conn.isClosed())	
				System.out.println("Succeeded connecting to the Database!");
			bookServerDB = new BookServerDB(conn);
			customerServerDB = new CustomerServerDB(conn);
			orderServerDB = new OrderServerDB(conn);
			managerServerDB = new ManagerServerDB(conn);
			promotionServerDB = new PromotionServerDB(conn);
		}catch(ClassNotFoundException e){
			System.out.println("Sorry,can`t find the Driver!");
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}  
	}
	
	public static BookServerInterface getBookServerDB(){
		return bookServerDB;
	}
	
	public static CustomerServerInterface getCustomerServerDB(){
		return customerServerDB;
	}
	
	public static OrderServerInterface getOrderServerDB(){
		return orderServerDB;
	}
	
	public static ManagerServerInterface getManagerServerDB(){
		return managerServerDB;
	}
	
	public static PromotionServerInterface getPromotionServerDB(){
		return promotionServerDB;
	}
}
